package com.lmqb.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lmqb.bean.SysRole;
import com.lmqb.bean.SysUser;
import com.lmqb.constant.MsgError;
import com.lmqb.entry.MenuTitle;
import com.lmqb.page.SysUserLogPager;
import com.lmqb.page.SysUserLoginPager;
import com.lmqb.responces.ResultData;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.RoleService;
import com.lmqb.service.SysUserService;
import com.lmqb.util.StrUtil;

@Controller
public class SysUserController extends BaseController{

	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private JiaoYanService jiaoYanService;
	
	//跳转到管理员列表界面
	@RequestMapping (value="sysUser.html",method=RequestMethod.GET)
	public String sysUserList(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,SysUser admin){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		if(admin==null){
			admin=new SysUser();
		}
		List<SysUser> list=sysUserService.selectAll(admin);
		request.setAttribute(AdminListF, list);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("管理员管理");
		session.setAttribute(menuTitleF, menuTitle);
		return "sysUserList";
	}
	
	//跳转到添加管理员界面
	@RequestMapping (value="addSysUser.html",method=RequestMethod.GET)
	public String toAddSysUser(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		SysRole sysRole=new SysRole();
		sysRole.setRoleStatus((byte)1);
		List<SysRole> list=roleService.selectAll(sysRole);
		request.setAttribute(AllSysRoleF, list);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("添加管理员");
		session.setAttribute(menuTitleF, menuTitle);
		return "addSysUser";
	}
	
	//添加管理员
	@RequestMapping(value="addSysUser",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> addSysUser(HttpSession session,HttpServletRequest request,
			HttpServletResponse respones,String loginName,String pwd,String pwd1,
			String realName,Byte deleteFlag,Integer roleId,String mobile,
			String job){
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			SysUser sysUser1=new SysUser();
			sysUser1.setDeleteFlag(deleteFlag);
			sysUser1.setLoginName(loginName);
			sysUser1.setRealName(realName);
			sysUser1.setUserPwd(pwd);
			sysUser1.setRoleId(roleId);
			sysUser1.setJob(job);
			sysUser1.setMobile(mobile);
			resultData=jiaoYanService.addSysUser(sysUser1,pwd1);
			if(resultData.getStatus()==1){
				return resultData;
			}
			sysUserService.insert(sysUser1,sysUser);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	//跳转到修改管理员界面
	@RequestMapping (value="modifySysUser.html",method=RequestMethod.GET)
	public String toModifySysUser(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,Integer userId){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		SysUser sysUser1=sysUserService.selectById(userId);
		request.setAttribute(SysUserF, sysUser1);
		SysRole sysRole=new SysRole();
		sysRole.setRoleStatus((byte)1);
		List<SysRole> list=roleService.selectAll(sysRole);
		request.setAttribute(AllSysRoleF, list);
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("系统配置");
		menuTitle.setMenu("修改管理员");
		session.setAttribute(menuTitleF, menuTitle);
		return "modifySysUser";
	}
	
	//修改管理员
	@RequestMapping(value="modifySysUser",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> modifySysUser(HttpSession session,HttpServletRequest request,
			HttpServletResponse respones,String loginName,String pwd,String pwd1,
			String realName,Byte deleteFlag,Integer roleId,
			Byte userStatus,Integer userId,String mobile,String job){
		System.out.println("*******2017.08.15"+pwd+"-->"+pwd1);
		ResultData<String> resultData=new ResultData<String>();
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				resultData.setNotLogin();
				return resultData;
			}
			SysUser sysUser1=new SysUser();
			sysUser1.setUserId(userId);
			sysUser1.setDeleteFlag(deleteFlag);
			sysUser1.setLoginName(loginName);
			sysUser1.setRealName(realName);
			sysUser1.setUserPwd(pwd);
			sysUser1.setRoleId(roleId);
			sysUser1.setUserStatus(userStatus);
			sysUser1.setJob(job);
			sysUser1.setMobile(mobile);
			System.out.println(sysUser1+"-->"+pwd1);
			resultData=jiaoYanService.modifySysUser(sysUser1,pwd1);
			if(resultData.getStatus()==1){
				return resultData;
			}
			sysUserService.update(sysUser1);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	/*
	 * 跳转到获取管理员操作日志界面
	 * */
	@RequestMapping (value="sysUserLog.html",method=RequestMethod.GET)
	public String sysUserLog(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,SysUserLogPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(SysUserLogPager)jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			SysUserLogPager logPager=sysUserService.selectLogPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, logPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("个人操作");
			menuTitle.setMenu("操作日志");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		return "sysUserLogList";
	}
	
	//跳转到获取管理员登录日志界面
	@RequestMapping (value="sysUserLogin.html",method=RequestMethod.GET)
	public String sysUserLogin(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,SysUserLoginPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(SysUserLoginPager)jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			SysUserLoginPager logPager=sysUserService.selectLoginPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, logPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("日志管理");
			menuTitle.setMenu("管理员登录");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "sysUserLoginList";
	}
	
	//管理员退出登录
	@RequestMapping (value="logOut",method=RequestMethod.GET)
	public String logOut(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		session.removeAttribute(memberAdmin);
		return "login";
	}
}
